<?php

class user extends ebase
{  
	/**
	 * 用户登录
	 */
	function login()
	{
		app::load(ROOT."/config.inc.php");
		
 		app::load(ROOT.'/uc_client/client.php'); 
 		
 		$result = uc_user_login($_POST['username'], $_POST['password']);
 		
 		return $result;
	} 
	/**
	 * 用户名AJAX检查
	 * @param username 用户名 $string
	 * @return 返回 1.可以使用； -1.用户名不合法；-2.用户名包含禁止使用字符；-3.用户名已存在
	 */
	function ajaxCheckUserName($username)
	{  
		app::load(ROOT."/config.inc.php");
 		app::load(ROOT.'/uc_client/client.php');  
 		$r1 = uc_user_checkname($username); 
	 	return $r1;		
	}
	/**
	 * 新增用户
	 * @param userArr 用户资料  数组类型  格式：字段=>值 
	 */
	function add($userArr)
	{
		return $this->db->insert($GLOBALS['cfg']['tablepre']."users", $userArr);
	}
	/**
	 * 载入flash控件
	 * @param int $uid
	 * @return string html字符串。flash控件
	 */
	function avatar_flash($uid){
		app::load(ROOT."/config.inc.php");
 		app::load(ROOT.'/uc_client/client.php');  
		$uc_avatarflash = uc_avatar($uid, (empty($_SCONFIG['avatarreal'])?'virtual':'real'));
		return $uc_avatarflash;
	}

	//处理头像
	function get_avatar($uid, $size='small', $returnsrc = FALSE) {
		app::load(ROOT."/config.inc.php");
 		app::load(ROOT.'/uc_client/client.php');  
		$size = in_array($size, array('big', 'middle', 'small')) ? $size : 'small';
		$avatarfile = $this->avatar_file($uid, $size);
		return $returnsrc ? UC_API.'/data/avatar/'.$avatarfile : '<img src="'.UC_API.'/data/avatar/'.$avatarfile.'" onerror="this.onerror=null;this.src=\''.UC_API.'/images/noavatar_'.$size.'.gif\'">';
	}
	//得到头像
    private function avatar_file($uid, $size) {
		$type = empty($_SCONFIG['avatarreal'])?'virtual':'real';
		$var = "avatarfile_{$uid}_{$size}_{$type}";
		if(empty($_SGLOBAL[$var])) {
			$uid = abs(intval($uid));
			$uid = sprintf("%09d", $uid);
			$dir1 = substr($uid, 0, 3);
			$dir2 = substr($uid, 3, 2);
			$dir3 = substr($uid, 5, 2);
			$typeadd = $type == 'real' ? '_real' : '';
			$_SGLOBAL[$var] = $dir1.'/'.$dir2.'/'.$dir3.'/'.substr($uid, -2).$typeadd."_avatar_$size.jpg";
		}
		return $_SGLOBAL[$var];
	}
	/**
	 * 删除用户，
	 * @param array $uids //用户id数组
	 */
	function del($uids)
	{
		app::load(ROOT."/config.inc.php");
 		app::load(ROOT.'/uc_client/client.php');
 		$rl = uc_user_delete($uids);
		return $this->db->delete($GLOBALS['cfg']['tablepre']."users","id in (".implode(",", $uids).") and groupid<>1");
	}
	/**
	 * 获取用户list
	 * @param string $sql
	 * @param int $limit 每页条数
	 * @param int $start 从第$start开始取
	 * @return array("count"=>int(10),'data'=>array())
	 */
	function getlist($sql,$limit,$start)
	{
		return $this->db->getList($sql,$limit,$start);
	}
	//获取某个用户信息
	function getUserInfo($id)
	{
		$info= $this->db->getOne("select u.*,ug.allowact,ug.groupname from ".$GLOBALS['cfg']['tablepre']."users u left join ".$GLOBALS['cfg']['tablepre']."usergroup
		 ug on ug.id=u.groupid where u.id='$id'");
		if($info[allowact])
		{
			$info[allowact].=',';
		}
		$info[allowact].='main,project';
		return $info;
	}
	function getUserName($id)
	{
		$info= $this->db->getOne("select u.*,ug.allowact,ug.groupname from ".$GLOBALS['cfg']['tablepre']."users u left join ".$GLOBALS['cfg']['tablepre']."usergroup
		 ug on ug.id=u.groupid where u.id='$id'");
		return array($info[username],$info[realname]);
	}
	
	function getuidlist($usernames,$uid,$model=0)
	{
		$msgto=array();
		$muids=array();
		if(trim($usernames))
		{
			$usernames=$usernames;
			$usernames=preg_replace("/,/", "','", $usernames);
			
			$info= $this->db->getAll("select username,id from ".$GLOBALS['cfg']['tablepre']."users where username in ('".$usernames."') and id<>'$uid'");
			foreach($info as $val)
			{
				$muids[]=$val[id];
				$msgto[]=$val[username];
			}
			if($msgto)
			{
				if(!$model){
					$msgto=implode(",", $msgto);
				}else{
					$msgto=$muids;
				}
			}
			
		}
		return $msgto;
	}
	
	/**
	 * 与uc同步检测或者检测用户是否注册
	 * @param string $username
	 * @param string $pass
	 */
	function checkUser($username,$pass){
		
		return $this->db->getOne('select * from '.$GLOBALS['cfg']['tablepre']."users"." where username='$username' and password='".md5($pass)."'");
	}
	function uc_add($username,$password,$email)
	{
		app::load(ROOT."/config.inc.php");
 		app::load(ROOT.'/uc_client/client.php');  
 		$r1 = uc_user_register($username,$password,$email); 
	 	return $r1;		
	}
	
	function ajaxCheckEmail($email){
		app::load(ROOT."/config.inc.php");
 		app::load(ROOT.'/uc_client/client.php');  
 		$r1 = uc_user_checkemail($email); 
	 	return $r1;		
	}
	/**
	 * 是有有权限操作该类动作
	 * @param string $act 操作的动作
	 * @param int $uid 用户id
	 * @return bool true 有权限操作，false没权限
	 */
	function checkUserAllow($act,$uid)
	{
		$member=$this->getUserInfo($uid);
		$allow=explode(',', $member['allowact']);
		if(in_array('all', $allow))
		{
			return true;
		}
		if(in_array('senior', $allow))
		{
			return true;
		}
		if(in_array($act, $allow))
		{
			return true;
		}
		return false;
	}
	
	/**
	 * 该用户允许执行的模块
	 * @param string $allowact
	 */
	function checkAllowModel($allowact)
	{
		$allow=explode(',', $allowact);
		if(in_array('all', $allow))
		{
			return 1;
		}
		if(in_array('senior', $allow))
		{
			return 3;
		}
		if($allow[0]=='normal')
		{
			return 2;
		}
		if($allow)
		{
			$atr=array();
			foreach($allow as $vl)
			{
				$atr[$vl]=$vl;
			}
			return $atr;
		}
		return 2;
	}
	
	/**
	 * 获取是否管理员
	 * @param string $allow 操作权限
	 */
	function checkUserType($allow){
		$array=explode(',', $allow);
		if(in_array('all', $array))
		{
			return true;
		}
		if(in_array('senior',$array))
		{
			return true;
		}
		return false;
	}
	function edit($params,$where)
	{
		return $this->db->update($GLOBALS['cfg']['tablepre']."users", $params,$where);
	}
	function uc_edit($username,$oldpw,$newpw,$email,$act){
		app::load(ROOT."/config.inc.php");
 		app::load(ROOT.'/uc_client/client.php');  
 		$r1;
 		if($act==1||$act==2)
 		{
 			$r1 = uc_user_edit1($username,$oldpw,$newpw,$email); 
 		}else{
 			$r1 = uc_user_edit($username,$oldpw,$newpw,$email); 
 		}
 		
	 	return $r1;		
	}
	function getGroup($uid)
	{
		$member=$this->getUserInfo($uid);
		return intval($member[groupid]);
	}
	function getGroupList(){
		$data=$this->db->getList("select * from ".$GLOBALS['cfg']['tablepre']."usergroup",1000,0);
		return $data['data'];
	}
	/**
	 * 获取权限列表不分页
	 * @param string $acts 一个权限或者是一组权限，以“,”隔开
	 * @return array $result; 返回权限数组
	 */
	function getPermissions($acts='')
	{
		if($acts){
			$allow=explode(",",$acts);
			$acts ="'".implode("','", $allow)."'";
			$data = $this->db->getAll("select * from ".$GLOBALS['cfg']['tablepre']."permissions where act in ($acts)");
		}else{
			$data = $this->db->getAll("select * from ".$GLOBALS['cfg']['tablepre']."permissions");	
		}
		return $data;
	}
}
?>